/**
 * 格式化输入
 * Created by KY on 2017/7/18.
 */
define(['ldfw/core','ldfw/utils/format'],function(LDFW){
    return LDFW.Component.extend({
        events:{
            'change':'changeHandler'
        },
        initialize:function(){
            var formatter = this.$el.attr('ld-format')
            this.formatter = formatter
            this.method = 'val'
            var targetTagName = this.$el.prop("tagName");
            if (targetTagName == "INPUT") {
            }else{
                this.method = 'text'
            }
        },
        changeHandler:function(){
            var val = this.$el.val();
            val = val.replace(/[^-1234567890.]+/g,'')
            this.setValue(val);
        },
        setValue:function(value){
            if(value){
                value = parseFloat(value)
            }
            $.fn[this.method].call(this.$el,format('number',this.formatter,value))
        },
        getValue:function(){
            var value = this.$el.val().replace(/,/g,'')
            if(value){
                return parseFloat(value)
            }else{
                return value
            }
        }
    })
})